<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Edit;
use addons\BuilderPlus\facade\Table;
use think\facade\Cache;
use think\Response;

class UserMenu extends Base
{
    public function getOptions(): array
    {
        return [
            'pk' => 'id',
            'model' => \app\common\model\UserMenu::class,
            'with' => ['children']
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->setCheckbox(false)
            ->addColumns([
                ['title', '标题', fn(Column $col) => $col->align('left'), fn(Edit $e) => $e->require()],
                ['subtitle', '副标题', null, fn(Edit $e) => $e->text()],
                ['icon', '图标', fn(Column $col) => $col->lazyImage()->width(100), fn(Edit $e) => $e->images()],
                ['status', '状态', fn(Column $col) => $col->switch('开启|关闭', '1|0')],
                ['sort', '排序', fn(Column $col) => $col->fastSort(), fn(Edit $e) => $e->number()],
                ['create_time', '添加时间', fn(Column $col) => $col->width(150)],
                ['update_time', '更新时间', fn(Column $col) => $col->width(150)]
            ])
            ->addRightButtons(['edit'], 100)
            ->enableTree(false, true)
            ->setSortType(['id' => 'asc'])
            ->fetch();
    }

    public function update(): Response
    {
        Cache::delete('userMenuList');

        return parent::update();
    }

    public function save(array $param = [], string $method = ''): Response
    {
        Cache::delete('userMenuList');

        return parent::save($param, $method);
    }
}